package com.atm.atmsys.controller;

import com.atm.atmsys.Service.AccountService;
import com.atm.atmsys.Service.AdminService;
import com.atm.atmsys.Service.UserService;
import com.atm.atmsys.dto.ATMLoginDTO;
import com.atm.atmsys.entity.AccountInfo;
import com.atm.atmsys.entity.AdminInfo;
import com.atm.atmsys.entity.UserInfo;
import com.atm.atmsys.utils.JwtUtil;
import com.atm.atmsys.utils.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class LoginController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private UserService userService;

    @Autowired
    private AccountService accountService;

    @PostMapping("/login/admin")
    @ApiOperation("银行管理员登录")
    public Result login(@RequestBody AdminInfo adminInfo,HttpServletRequest request){
        log.info("银行管理员登录：{}",adminInfo);
        AdminInfo adminInfo1=adminService.login(adminInfo);

        //登录成功，发令牌
        if(adminInfo!=null){
            Map<String, Object> claims=new HashMap<>();
            claims.put("adminId",adminInfo1.getAdminId());
            claims.put("adminName",adminInfo1.getAdminName());

            String jwt = JwtUtil.generateJWT(claims);
            return Result.success(jwt);
        }
        request.getSession().setAttribute("adminId",adminInfo1.getAdminId());
        return Result.error("用户名或密码错误");
    }


    //管理员登出
    @ApiOperation("管理员登出")
    @PostMapping("/admin/logout")
    public Result<String> Adminlogout(HttpServletRequest request) {
        //清理session
        request.getSession().removeAttribute("accountId");
        //返回操作成功
        return Result.success("退出成功");
    }

    //用户登录
    @PostMapping("/login/user")
    @ApiOperation("用户登录")
    public Result login(@RequestBody UserInfo userInfo, HttpServletRequest request){
        log.info("用户登录：{}",userInfo);
        UserInfo userInfo1=userService.login(userInfo);

        //登录成功，发令牌
        if(userInfo!=null){
            Map<String, Object> claims=new HashMap<>();
            claims.put("userId",userInfo1.getUserId());
            claims.put("userName",userInfo1.getUserName());

            String jwt = JwtUtil.generateJWT(claims);
            return Result.success(jwt);
        }
        request.getSession().setAttribute("UserId",userInfo1.getUserId());
        return Result.error("用户名或密码错误");
    }

    //用户登出
    @ApiOperation("用户退出登录")
    @PostMapping("/employee/logout")
    public Result<String> Userlogout(HttpServletRequest request) {
        //清理session
        request.getSession().removeAttribute("UserId");
        //返回操作成功
        return Result.success("退出成功");
    }


    //ATM登录
    @PostMapping("/login/atm")
    @ApiOperation("ATM登录")
    public Result login(@RequestBody ATMLoginDTO atmLoginDTO,HttpServletRequest request){
        log.info("ATM登录：{}",atmLoginDTO);
        AccountInfo accountInfo1=accountService.login(atmLoginDTO);
        System.out.println(accountInfo1);
        //登录成功，发令牌
        if(accountInfo1!=null){
            Map<String, Object> claims=new HashMap<>();
            claims.put("accountId",accountInfo1.getAccountId());
            claims.put("cardNo",accountInfo1.getCardNo());
            String jwt = JwtUtil.generateJWT(claims);
            request.getSession().setAttribute("accountId",accountInfo1.getAccountId());
            request.getSession().setAttribute("atmId",atmLoginDTO.getAtmId());
            return Result.success(jwt);
        }

        return Result.error("密码错误");
    }

    //ATM登出
    @ApiOperation("ATM登出")
    @PostMapping("/atm/logout")
    public Result<String> ATMlogout(HttpServletRequest request) {
        //清理session
        request.getSession().removeAttribute("accountId");
        //返回操作成功
        return Result.success("退出成功");
    }

}
